package me.rvj.servlet;

import me.rvj.dao.UserDao;
import me.rvj.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @program: JavaWeb
 * @description: 检查用户名是否已使用
 * @author: Rv_Jiang
 * @date: 2021/7/2 9:46
 */
@WebServlet(name = "CheakUserServlet",value = "/CheakUserServlet")
public class CheakUserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userName = req.getParameter("userName").trim();
        if (userName.isEmpty()){
            resp.setStatus(404);
        }

        List<User> userList= UserDao.getUserList();
        for (User user: userList) {
            if(userName.equals(user.getUserName())){
                resp.getWriter().print(true);
                return;
            }
        }
        resp.getWriter().print(false);
//        resp.sendError(404,"未能找到用户");


    }

}
